package models

import (
	"exp/appConfig"
	"fmt"
	mysql2 "gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	DB *gorm.DB
)

func Init() {
	var err error

	mysql := appConfig.Config.Mysql

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysql.User, mysql.Password, mysql.Host, mysql.Port, mysql.Database)
	DB, err = gorm.Open(mysql2.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql 连接成功")

	err = DB.AutoMigrate(&User{}, &UserRole{}, &Role{}, &RoleNode{}, &Node{}, &Comp{}, &Express{}, &Order{})
	if err != nil {
		panic(err)
	}
	fmt.Println("迁移成功")
}
